package org.szy.plugin.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.mybatisflex.core.paginate.Page;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.core.common.param.CommonMoveParam;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.plugin.sys.entity.SysOrg;
import org.szy.plugin.sys.entity.table.SysOrgTableDef;
import org.szy.plugin.sys.param.SysOrgQueryParam;
import org.szy.plugin.sys.pojo.SysOrgTree;
import org.szy.plugin.sys.pojo.SysOrgTreeOption;
import org.szy.plugin.sys.service.SysOrgService;

import java.util.List;

/**
 * 系统组织控制器
 *
 * @author wangbuhao
 * @since 2023-06-18 17:12:42
 */
@Controller
@Mapping("/sys/org")
public class SysOrgController extends BaseController<SysOrg> {

    private final String PATH_PREFIX = "sys/org/";
    @Inject
    private SysOrgService sysOrgService;

    /**
     * 系统组织列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        boolean exists = sysOrgService.exists(SysOrgTableDef.SYS_ORG.CODE.eq(null));
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 系统组织编辑页
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "edit");
        return view;
    }

    /**
     * 系统组织详情页
     */
    @Get
    @Mapping("/detail")
    public ModelAndView detail() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "detail");
        return view;
    }

    /**
     * 获取系统组织树形数据
     *
     * @author wangbuhao
     * @since 2023-06-18 17:12:42
     */
    @Get
    @Mapping("/treeList")
    @SaCheckPermission(value = "sys_org_list", mode = SaMode.OR, orRole = "wangbuhao")
    public R<List<SysOrgTree>> treeList(SysOrgQueryParam param) {
        List<SysOrg> list = sysOrgService.list(param);
        List<SysOrgTree> trees = sysOrgService.buildTrees(list);
        return R.data(trees);
    }

    /**
     * 获取组织数据结构选项
     * @param param 查询参数
     * @return
     */
    @Get
    @Mapping("/treeOption")
    public R<List<SysOrgTreeOption>> treeOption(SysOrgQueryParam param) {
        List<SysOrg> list = sysOrgService.list(param);
        List<SysOrgTree> trees = sysOrgService.buildTrees(list);
        List<SysOrgTreeOption> treeOption = SysOrgTreeOption.buildTreeOption(trees);
        return R.data(treeOption);
    }

    /**
     * 获取系统组织详情
     *
     * @author wangbuhao
     * @since 2023-06-18 17:12:42
     */
    @Get
    @Mapping("/getInfo")
    @SaCheckPermission(value = "sys_org_detail", mode = SaMode.OR, orRole = "wangbuhao")
    public R<SysOrg> getInfo(Long id) {
        return R.asNull(sysOrgService.getById(id));
    }

    /**
     * 添加系统组织
     *
     * @author wangbuhao
     * @since 2023-06-18 17:12:42
     */
    @Post
    @Mapping("/add")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_org_add", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> add(@Validated SysOrg entity) {
        boolean save = sysOrgService.add(entity);
        return save ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 编辑系统组织
     *
     * @author wangbuhao
     * @since 2023-06-18 17:12:42
     */
    @Put
    @Mapping("/update")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_org_update", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> update(@Validated SysOrg entity) {
        boolean update = sysOrgService.edit(entity);
        return R.bool(update);
    }

    /**
     * 删除系统组织
     *
     * @author wangbuhao
     * @since 2023-06-18 17:12:42
     */
    @Delete
    @Mapping("/delete")
    @SaCheckPermission(value = "sys_org_delete", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> delete(@NotNull Long id) {
        boolean delete = sysOrgService.deleteById(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * 修改状态
     *
     * @author wangbuhao
     * @date 2023年2月13日21:02:00
     */
    @Put
    @Mapping("/modifyStatus")
    public R<?> modifyStatus(@Validated CommonStatusParam statusParam) {
        boolean status = sysOrgService.modifyStatus(statusParam);
        return R.bool(status);
    }

    /**
     * 移动组织
     *
     * @author wangbuhao
     * @date 2023年2月15日10:25:15
     */
    @Put
    @Mapping("/move")
    public R<?> move(CommonMoveParam commonMoveParam) {
        boolean move = sysOrgService.move(commonMoveParam);
        return R.bool(move);
    }
}
